package com.xjc.business.controller.stu;

import com.xjc.business.entity.po.stu.ActivityCommentsPo;
import com.xjc.business.entity.po.stu.ActivityJoinPo;
import com.xjc.business.entity.vo.ActivityRankVo;
import com.xjc.business.entity.vo.stu.*;
import com.xjc.business.service.ActivitiesService;
import com.xjc.business.service.ActivityCommentsService;
import com.xjc.business.service.ActivityJoinListsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 徐锦程
 * 时间:2025-02-10 16:30
 */
@RestController
@RequestMapping("/student/activity")
public class StuActivityController {

    @Autowired
    ActivitiesService activitiesService;

    @Autowired
    ActivityJoinListsService activityJoinListsService;

    @Autowired
    ActivityCommentsService activityCommentsService;

    /**
     * 获取宿舍评优活动列表
     * @param wxId 微信id
     * @param level 级别
     * @return Result
     */
    @GetMapping("/List/{wxId}/{level}")
    public Result<List<ActivityVo>> getActivityListWithLevel(
            @PathVariable(value = "wxId",required = false) Integer wxId,
            @PathVariable(value = "level",required = false) Integer level
    ){
        if(level == null || wxId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activitiesService.getActivityListWithLevel(wxId,level);
    }

    /**
     * 获取宿舍评优内容
     * @param id 宿舍评优id
     * @return 内容
     */
    @RequestMapping("/desc/{id}")
    public Result<String> getActivityDesc(@PathVariable(value = "id",required = false) Integer id){
        return Result.okWithData(activitiesService.getById(id).getContent());
    }

    /**
     * 获取宿舍评优活动参与列表
     * @param id 宿舍评优id
     * @return Result
     */
    @GetMapping("/joinList/{id}")
    public Result<List<ActivityJoinVo>> getActivityJoinList( @PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activityJoinListsService.getActivityJoinListAtStu(id);
    }

    /**
     * 获取宿舍评优活动排名列表-前十
     * @param id 宿舍评优id
     * @return Result
     */
    @GetMapping("/rankList/{id}")
    public Result<List<ActivityRankVo>> getActivityRankList(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activityJoinListsService.getActivityRankList(id);
    }

    /**
     * 获取宿舍评优活动评论列表
     * @param id 宿舍评优活动参加记录id
     * @return Result
     */
    @GetMapping("/commentsList/{id}")
    public Result<List<ActivityCommentsVo>> getActivityCommentsList(@PathVariable(value = "id",required = false) Integer id) {
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activityCommentsService.getActivityCommentsList(id);
    }

    /**
     * 添加宿舍评优活动评论
     * @param activityCommentsPo 评论内容
     * @return Result
     */
    @PostMapping("/addComment")
    public Result<String> addComment(@RequestBody(required = false)ActivityCommentsPo activityCommentsPo){
        if(activityCommentsPo == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activityCommentsService.addComment(activityCommentsPo);
    }

    /**
     * 判断活动是否结束
     * @param id 宿舍评优活动参加记录id
     * @return Result
     */
    @GetMapping("/isTimeOut/{id}")
    public Result<Boolean> isTimeOut(@PathVariable(value = "id",required = false) Integer id){
        if(id == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activitiesService.isTimeOut(id);
    }

    /**
     * 参加宿舍评优活动
     * @param activityJoinPo 信息
     * @return Result
     */
    @PostMapping("/joinActivity")
    public Result<String> joinActivity(@RequestBody(required = false)ActivityJoinPo activityJoinPo){
        if(activityJoinPo == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activityJoinListsService.joinActivity(activityJoinPo);
    }

    /**
     * 判断是否已经参加
     * @param activityId 宿舍评优活动id
     * @param wxId 微信id
     * @return Result
     */
    @GetMapping("/hasJoined/{activityId}/{wxId}")
    public Result<Boolean> hasJoined(@PathVariable(value = "activityId",required = false) Integer activityId,@PathVariable(value = "wxId",required = false) Integer wxId){
        if(activityId == null || wxId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        return activityJoinListsService.hasJoined(activityId,wxId);
    }

    /**
     * 获取当前参加信息
     * @param activityId 宿舍评优活动id
     * @param wxId 微信id
     * @return Result
     */
    @GetMapping("/getCurrentJoinInfo/{activityId}/{wxId}")
    public Result<ActivityCurrentJoinVo> getCurrentJoinInfo(@PathVariable(value = "activityId",required = false) Integer activityId,@PathVariable(value = "wxId",required = false) Integer wxId){
        if(activityId == null || wxId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return activityJoinListsService.getCurrentJoinInfo(activityId,wxId);
    }
}